package Synchronized;

public class demo2 {
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Thread t1 = new Thread(()->{

            synchronized (locker)
            {
                System.out.println(Thread.currentThread().getName()+"抢到锁进行加锁....");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"解锁后轮到下一个线程抢占执行....");
            }

        },"线程1");
        Thread t2 = new Thread(()->{

            synchronized (locker)
            {
                System.out.println(Thread.currentThread().getName()+"抢到锁进行加锁....");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"解锁后轮到下一个线程抢占执行....");
            }

        },"线程2");

        t1.start();
        t2.start();
    }
}
